package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.PersonalAttentionBean;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

/**
 * Created by Jinni on 2017/7/31 0031.
 */

public class PersonalAttentionAdapter extends BaseRecyclerViewAdapter<PersonalAttentionBean.DataBean.FormBean> {

    private Context mContext;
    private OnItemClickListener mListener;

    public void setListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public PersonalAttentionAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_my_attention_rectangle, null);
        return new itemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        final PersonalAttentionBean.DataBean.FormBean bean = mList.get(position);
        RequestOptions requestOptions = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .override(350, 350);
        Glide.with(mContext.getApplicationContext())
                .load(TextUtils.isEmpty(bean.getPhoto()) ? R.mipmap.default_avatar : HttpRequestConstant.USER_PIC_HEAD + bean.getPhoto())
                .apply(requestOptions)
                .into(((itemViewHolder) holder).avatar);
        ((itemViewHolder) holder).university.setText(bean.getUnit());
        ((itemViewHolder) holder).username.setText(bean.getName());
        ((itemViewHolder) holder).mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mListener.onItemClickListener(holder.getAdapterPosition(), bean.getFromUid(), bean.getToUid());
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mList != null) {
            return mList.size();
        } else {
            return 0;
        }
    }

    private class itemViewHolder extends RecyclerView.ViewHolder {

        private CircleImageView avatar;
        private TextView        university;
        private TextView        username;
        private View            mView;

        private itemViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            avatar = (CircleImageView) itemView.findViewById(R.id.civ_user_avatar);
            university = (TextView) itemView.findViewById(R.id.tv_user_university);
            username = (TextView) itemView.findViewById(R.id.tv_user_nickname);
        }
    }

    public interface OnItemClickListener{
        void onItemClickListener(int position, String uid, String toUid);
    }
}